/**
 * This is a file comment
 */

import java.io.Serializable;

import javax.swing.JOptionPane;

/**
 * Creates the SpacePort, which allows the User to purchase fuel.
 * 
 * @author Taylor Garren
 * @version 1.0
 */

public class SpacePort implements Serializable {
    
    /**
     * The user
     */
    
    private final Player player = Game.Player;
    
    /**
     * The player's ship
     */
    
    private final Ship ship = player.getShip();
    
    /**
     * The cost per unit of fuel
     */
    
    private static final double FUEL_COST = 3.0;

    /**
     * Adds the given units of fuel to the ship if the player meets certain
     * criteria. If too much fuel was selected, the tank is filled to capacity
     * and only the price of those units are subtracted from the player's
     * wallet. If the player does not have the money to complete the action, no
     * fuel is added. A message will be displayed at the end letting the user
     * know the state of the transaction.
     * 
     * @param unitsFuelAdd
     */
    public void fuelShip(int unitsFuelAdd) {
	String message = "";
	final double wallet = player.getWallet();
	int currentFuel = ship.getCurrFuelLevel();
	final int tankCapacity = ship.getThisFuelSize();
	int actualAddedFuel = unitsFuelAdd;
	boolean overflow = false;
	if (!ship.canAddFuel(unitsFuelAdd)) {
	    actualAddedFuel = tankCapacity - currentFuel;
	    overflow = true;
	}
	final double cost = actualAddedFuel * FUEL_COST;
	if (canFuel(cost, wallet)) {
	    player.setWallet(wallet - cost);
	    ship.addFuel(actualAddedFuel);
	    if (overflow) {
		message = "You tried to add " + unitsFuelAdd
			+ " units of fuel to a tank with " + currentFuel + "/"
			+ tankCapacity + " units of fuel. " + actualAddedFuel
			+ " units were added. ";
	    } else {
		message = "You added " + unitsFuelAdd
			+ " units of fuel to a tank with " + currentFuel + "/"
			+ tankCapacity + " units of fuel. ";
	    }
	    currentFuel = ship.getCurrFuelLevel();
	    message += "\nYour tank has " + currentFuel + "/" + tankCapacity
		    + " units of fuel. You " + "started out with " + wallet
		    + " credits, you have " + (wallet - cost)
		    + " \ncredits left.";
	} else {
	    message = "Insufficient funds to buy " + actualAddedFuel
		    + " units of fuel at a " + "cost of " + cost
		    + " credits. You have " + (wallet - cost)
		    + " credits in your wallet.";
	}
	JOptionPane
		.showMessageDialog(Game.InitConfigScreen.getFrame(), message);

    }

    /**
     * Returns a boolean that indicates whether or not the player had the
     * credits to pay for the requested fuel - true if the user did, false if
     * the user did not
     * 
     * @param cost
     *            The total cost of the fuel the user wishes to purchase
     * @param wallet
     *            The total amount of credits held by the player
     * @return True if the player can purchase the fuel, false if he or she
     *         cannot
     */
    public boolean canFuel(double cost, double wallet) {
	return wallet - cost > 0;
    }
    
    /**
     * @return The results of the default toString() method for a class
     */
    public String toString() {
	return this.toString();
    }

}
